<template>
  <div class="question">
    <!-- 顶部区域 -->
    <van-dropdown-menu>
      <van-dropdown-item ref="menuItem">
        <template #title>
          <h3 class="title">
            面试宝典<span>{{ city }}</span>
          </h3>
        </template>
        <van-index-bar>
          <!-- 使用template遍历，key不能绑定在template上而是绑定在下面，因为template运行后会消失 -->
          <template v-for="(value, key, indexs) in cities">
            <van-index-anchor
              style="background-Color:#f7f4f5;"
              :index="key"
              :key="indexs"
            />
            <van-cell
              :title="item"
              v-for="(item, index) in value"
              :key="index"
              @click="selCity(item)"
            />
          </template>
        </van-index-bar>
      </van-dropdown-item>
    </van-dropdown-menu>
    <!-- 岗位区域 -->
    <div class="position">
      <div class="inner">
        <!-- 遍历选中的城市对应的岗位,被选中的岗位添加activa类名,岗位被点击，被选中的岗位的下标就等于被点击了的岗位的下标 -->
        <span
          :class="{ active: selIndex === index }"
          v-for="(item, index) in cityPositions[city]"
          :key="index"
          @click="selIndex = index"
          >{{ item.name }}</span
        >
      </div>
    </div>
    <!-- 刷题信息区域 -->
    <div class="quesInfo">
      <div class="item one">
        <i class="iconfont iconicon_mine_cuoti"></i>
        <span>常见错题</span>
      </div>
      <div class="item two">
        <i class="iconfont iconicon_mine_tikushoucang"></i>
        <span>收藏题库</span>
      </div>
      <div class="item three">
        <i class="iconfont iconicon_mine_qiyeshoucang"></i>
        <span>企业题库</span>
      </div>
      <div class="item four">
        <i class="iconfont iconicon_mine_mianjing"></i>
        <span>已答题库</span>
      </div>
      <div class="center">
        <van-circle
          class="circle"
          v-model="currentRate"
          layer-color="none"
          :color="gradientColor"
          :rate="rate"
          :speed="8"
          :stroke-width="60"
        >
          <template>
            <div class="word">
              <div class="top">顺序刷题</div>
              <div class="bottom">{{ targetAnswered }}/{{ targetTotal }}</div>
            </div>
          </template>
        </van-circle>
      </div>
    </div>
    <!-- 累计 -->
    <div class="total">
      <div class="txt">累计收录</div>
      <div class="num">{{ targetTotal }}</div>
    </div>
    <!-- 按钮 -->
    <div class="btn">
      <van-button @click="btnClick" class="an" size="large" color="#00b8d4"
        >模拟面试</van-button
      >
    </div>
  </div>
</template>
<script>
import { getFiltersApi } from '../../../api/interview.js'
// 导入vuex的辅助函数
import { mapState } from 'vuex'
export default {
  data () {
    return {
      cities: {} /* 城市列表 */,
      cityPositions: {} /* 岗位信息 */,
      city: '' /* 被选中的城市 */,
      selIndex: 0 /* 被选中的岗位的下标，默认第0个被选中 */,
      gradientColor: {
        /* 进度条的颜色 */
        '0%': '#f4a4b7',
        '100%': '#e40137'
      },
      currentRate: 0,
      total: 0 /* 总题数 */,
      targetTotal: 0 /* 目标总题数 */,
      targetAnswered: 0 /* 目标已达题数 */,
      // 进度条的弧度
      rate: 0
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  async created () {
    await this.getFilters()
    // 因为下面的total是根据上面的请求来的，所以等上面的异步请求执行完毕再执行下面的代码，否则下面代码执行时total是0
    this.rate =
      ((this.userInfo.correctQuestions.length +
        this.userInfo.errorQuestions.length) /
        this.total) *
      100
    const timer1 = setInterval(() => {
      this.targetTotal++
      // 判断是否到达总题数
      if (this.targetTotal >= this.total) {
        // 到达清除定时器
        clearInterval(timer1)
      }
    }, 3000 / this.total)
    const timer2 = setInterval(() => {
      this.targetAnswered++
      if (
        this.targetAnswered >=
        this.userInfo.correctQuestions.length +
          this.userInfo.errorQuestions.length
      ) {
        clearInterval(timer2)
      }
    }, 3000 / (this.userInfo.correctQuestions.length + this.userInfo.errorQuestions.length))
  },
  methods: {
    // 获取城市及岗位信息
    async getFilters () {
      const res = await getFiltersApi()
      // 保存信息并渲染
      this.cities = res.data.citys
      this.cityPositions = res.data.cityPositions
      // 开始的时候默认被选中的城市是全国
      this.city = this.cities['全国'][0]
      // 保存获取的总题数
      this.total = res.data.totalCount
    },
    // 下拉菜单的城市被选中时
    selCity (city) {
      // 接收当前被点击的城市名
      this.city = city
      // 关闭下拉菜单
      this.$refs.menuItem.toggle()
    },
    // 刷题按钮被点击
    btnClick () {
      // 获取当前选中城市和岗位对应的id
      // 请求城市和岗位信息需要事件，所以等他们都获取到了才使点击生效
      if (this.cityPositions && this.cityPositions[this.city]) {
        const positionID = this.cityPositions[this.city][this.selIndex].id
        // 跳转到刷题页面并携带城市和岗位id
        this.$router.push('/interview/' + this.city + '/' + positionID)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.question {
  background-color: #f7f4f5;
  height: 100vh;
  .van-dropdown-menu__bar {
    box-shadow: none;
  }
  .title {
    span {
      color: #545671;
      font-size: 12px;
      margin-left: 5px;
      font-weight: 400;
    }
  }
  .van-index-anchor--sticky {
    background-color: #f7f4f5;
  }
  .position {
    width: 100%;
    background-color: #fff;
    overflow: scroll;
    // 去除横向滚动条，伪类元素要加上&
    &::-webkit-scrollbar {
      display: none; /* Chrome Safari */
    }

    .inner {
      display: flex;
      padding: 10px 15px;
      .active {
        color: #fff;
        background-color: #00b8d4;
      }
      span {
        padding: 6px 14px;
        font-size: 12px;
        color: #545671;
        // 不让span压缩变形
        flex-shrink: 0;
        background-color: #f7f4f5;
        border-radius: 16px;
        margin-right: 16px;
      }
    }
  }
  .quesInfo {
    position: relative;
    height: 200px;
    background-color: #fff;
    margin: 15px 0;
    .item {
      position: absolute;
      font-size: 12px;
      display: flex;
      flex-direction: column;
      text-align: center;
      .iconfont {
        width: 44px;
        height: 44px;
        border-radius: 50%;
        font-size: 26px;
        line-height: 44px;
        color: #fff;
      }
      span {
        margin-top: 8px;
      }
    }
    .one {
      top: 17px;
      left: 42px;
      .iconfont {
        background: linear-gradient(180deg, #ff8080, #ff4949);
      }
    }
    .two {
      top: 17px;
      right: 42px;
      .iconfont {
        background: linear-gradient(180deg, #ffda05, #ffb302);
      }
    }
    .three {
      bottom: 17px;
      left: 42px;
      .iconfont {
        background: linear-gradient(180deg, #00ddec, #00b8d4);
      }
    }
    .four {
      bottom: 17px;
      right: 42px;
      .iconfont {
        background: linear-gradient(180deg, #3ee5b1, #1dc779);
      }
    }
    .center {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 119px;
      height: 119px;
      background: url(../../../assets/编组@2x.png);
      background-size: 100% 100%;
      overflow: hidden;
      box-sizing: border-box;
      .circle {
        width: 100%;
        height: 100%;
        padding: 10px;
        box-sizing: border-box;
        .word {
          font-size: 16px;
          color: #fff;
          border-radius: 50%;
          background: linear-gradient(180deg, #ff6f92, #e40137);
          height: 100%;
          overflow: hidden;
          .top {
            margin-top: 25px;
          }
          .bottom {
            margin-top: 10px;
          }
        }
      }
    }
  }
  .total {
    height: 130px;
    background-color: #fff;
    font-weight: 600;
    text-align: center;

    .txt {
      color: #545671;
      font-size: 18px;
      line-height: 65px;
      margin: 0 auto;
    }
    .num {
      font-size: 30px;
      color: #545671;
    }
  }
  .btn {
    padding: 15px;
    .an {
      border-radius: 6px;
    }
  }
}
</style>
